package foxmob.mooc.library;

/**
 * Created by liyusen on 2017/3/28.
 */


import foxmob.mooc.DbAccess;
import foxmob.mooc.persist.tables.Book;

import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import spark.Request;
import spark.Response;



import static spark.Spark.get;
import static spark.Spark.post;

// foxmooc
import static foxmob.mooc.Utils.getMultipart;




/**
 *
 * @author liyusen
 */

public class LibraryAction {

    public static void  init(){
        post("/book/add", LibraryAction::addBook);
        get("/books/get", LibraryAction::getBooks);
        post("/book/update",LibraryAction::updateBook);
        get("/book/del/:id",LibraryAction::delBook);

    }
    public static String addBook(Request req, Response res){
        int n = 0;

        String[] values = getMultipart(new String[]{"name","author","intro"},req);

        if(values.length!=3 || values[0]=="") return "0";

        try{
            DSLContext dsl = DbAccess.getDsl();


            Result<?> result = dsl.insertInto(Book.BOOK,Book.BOOK.NAME,Book.BOOK.AUTHOR,
                    Book.BOOK.INTRODUCTION).
                    values(values[0],values[1],values[2]).
                    returning(Book.BOOK.ID).fetch();
            for(Record r : result ){
                n = r.getValue(Book.BOOK.ID);
                break;
            }

        }
        catch (Exception e){
            e.printStackTrace();

        }
        /*
        try (InputStream is = req.raw().getPart("file").getInputStream()) {

            OutputStream os = new FileOutputStream("/Users/liyusen/wm/mini/foxmooc/a.pdf");
            IOUtils.copy(is,os);


        }catch(Exception e){
            //return "0";
        }
        */

        return String.format("%d",n);
    }
    public static  String updateBook(Request req,Response res){
        int n = 0;

        String[] values = getMultipart(new String[]{"id","name","author","intro"},req);

        if(values.length!=4 || values[0]=="") return "0";



        try{
            DSLContext dsl = DbAccess.getDsl();
            //Book Book.BOOK = new Book();

            n = dsl.update(Book.BOOK)
                    .set(Book.BOOK.NAME,values[1])
                    .set(Book.BOOK.AUTHOR,values[2])
                    .set(Book.BOOK.INTRODUCTION,values[3])
                    .where(Book.BOOK.ID.equal(Integer.parseInt(values[0])))
                    .execute();
        }
        catch (Exception e){
            e.printStackTrace();

        }

        return String.format("%d",n);
    }
    public  static String delBook(Request req, Response res){
        int n=0;
        try{
            DSLContext dsl = DbAccess.getDsl();
            //Book Book.BOOK = new Book();
            System.out.println("info\n"+req.params(":id"));

            n = dsl.deleteFrom(Book.BOOK).where(
                    Book.BOOK.ID.equal(
                            Integer.parseInt((req.params(":id"))))).execute();
        }
        catch (Exception e){
            e.printStackTrace();

        }
        return String.format("%d",n);
    }

    public static String getBooks(Request req, Response res){
        String rs = "[";
        int start = Integer.parseInt(req.queryParams("start"));
        int pgsize = Integer.parseInt(req.queryParams("pgsize"));
        String pattern = String.format("%s%s%s","%",req.queryParams("pattern"),"%");
        System.out.println(pattern);
        try{
            DSLContext dsl = DbAccess.getDsl();
            //Book Book.BOOK = new Book();
            Result<Record> result = dsl.select()
                    .from(Book.BOOK).where(Book.BOOK.NAME.like(pattern))
                    .or(Book.BOOK.AUTHOR.like(pattern))
                    .limit(pgsize).offset(start).fetch();

            for (Record r : result) {
                int bookid = r.getValue(Book.BOOK.ID);
                String author = r.getValue(Book.BOOK.AUTHOR);
                String name = r.getValue(Book.BOOK.NAME);
                String intro = r.getValue(Book.BOOK.INTRODUCTION);
                long date = r.getValue(Book.BOOK.BOUGHTDATE);
                String s = String.format("[%d,\"%s\",\"%s\",\"%s\"],",
                        bookid,name,author,intro);

                rs = String.format("%s%s",rs,s);
            }
            rs = String.format("%s[]]",rs);



    }catch(Exception e){
        e.printStackTrace();
        rs = "[[]]";

    }


        return rs;
    }

}
